package com.duleme.common;

import com.google.gson.Gson;

import java.sql.*;

/**
 * @标 题： 读了么
 * @项目名称： duleme
 * @描 述： TODO
 * @作 者： duanxi
 * @日 期： 2019/6/18  15:08
 */
public class DBUtils {
<<<<<<< HEAD:src/main/Java/com/ysuedu/common/DBUtils.java
    private static String url = "jdbc:mysql://localhost/bookshop?useSSL=false&characterEncoding=utf-8";
    private static String password = "";
    private static String user = "root";
=======
    private static String url = "jdbc:mysql://localhost/duleme?useSSL=false&characterEncoding=utf-8";
    private static String password = "qwerdf123";
    private static String user = "akiid";
>>>>>>> 任航:src/main/Java/com/duleme/common/DBUtils.java

    static {
        //1. 数据库驱动加载
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取对象
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    /**
     * 获取PreparedStatement对象
     *
     * @param sql
     * @return
     * @throws SQLException
     */
    public static PreparedStatement getPreparedStatement(String sql) throws SQLException {
        return getConnection().prepareStatement(sql);
    }

    /**
     * 关闭数据连接对象
     *
     * @param conn
     * @param ps
     * @param rs
     */
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {

        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void object2Json(Object o){
        Gson gson=new Gson();
        String json=gson.toJson(o);
         System.out.println(json);
    }
}
